﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using UniMana.Utils.Controls;
using System.IO;
using UniMana.Data;

namespace UniMana.Win.Personnel
{
    public partial class RptPersonel : DevExpress.XtraReports.UI.XtraReport
    {
        public RptPersonel(Guid personelKey)
        {
            InitializeComponent();
            #region [old]
            //this.DataSource = data;
            //xrlID.DataBindings.Add("Text", this.DataSource, "ID");
            //xrlLastName.DataBindings.Add("Text", this.DataSource, "LastName");
            //xrlLastName.DataBindings.Add("Text", this.DataSource, "Name"); 
            //xrlIdentityNo.DataBindings.Add("Text", this.DataSource,"IdentityNo");
            //xrlBirthday.DataBindings.Add("Text", this.DataSource, "Birthday", "{0:dd/MM/yyyy}");
            //xrlGender.DataBindings.Add("Text", this.DataSource, "Gender");
            //xrlPlaceOfBirth.DataBindings.Add("Text", this.DataSource, "PlaceOfBirth");
            //xrpPicture.DataBindings.Add("Image", this.DataSource, "Picture");
            //xrlPoliticsJoinDate.DataBindings.Add("Text", this.DataSource, "PoliticsJoinDate", "{0:dd/MM/yyyy}");
            //xrlCellPhone.DataBindings.Add("Text", this.DataSource, "CellPhone");
            //xrlHomePhone.DataBindings.Add("Text", this.DataSource, "HomePhone");
            //xrlAddr.DataBindings.Add("Text", this.DataSource, "Addr");
            //xrlTempAddr.DataBindings.Add("Text", this.DataSource, "TempAddr");
            //xrlEmail.DataBindings.Add("Text", this.DataSource, "Email");
            //xrlBeginDateWork.DataBindings.Add("Text", this.DataSource, "BeginDateWork", "{0:dd/MM/yyyy}");
            //xrlNotes.DataBindings.Add("Text", this.DataSource, "Notes");
            //xrlNation.DataBindings.Add("Text", this.DataSource, "Catalogue_NationKey");
            #endregion
            Personnel_PersonnelInfo info = new Personnel_PersonnelInfo(personelKey);
            xrlID.Text = info.Properties.ID;
            xrlLastName.Text = info.Properties.LastName + " " + info.Properties.Name;
            //Identity
            if (info.Properties.IdentityNo != null)
            {
                xrlIdentityNo.Text = info.Properties.IdentityNo;
            }
            else
            {
                xrlIdentityNo.Text = "";
            }
            //Birthday
            if (info.Properties.Birthday != null && info.Properties.Birthday.ToString() !="")
            {
                DateTime birthday;
                if (DateTime.TryParse(info.Properties.Birthday.Value.ToString(), out birthday))
                {
                    xrlBirthday.Text = birthday.ToShortDateString();
                }
                else
                {
                    xrlBirthday.Text = info.Properties.ExceptionBirthday.ToString();
                }
            }
            else
            {
                xrlBirthday.Text = info.Properties.ExceptionBirthday.ToString();
            }
            //Gender
            if(info.Properties.Gender.Value == true)
            {
                xrlGender.Text = "Nữ";
            }
            else
            {
                xrlGender.Text = "Nam";
            }
            //PlaceOfBirth
            if (info.Properties.PlaceOfBirth != null)
            {
                xrlPlaceOfBirth.Text = info.Properties.PlaceOfBirth;
            }
            else
            {
                xrlPlaceOfBirth.Text = "";
            }
            //Picture
            ////if (info.Properties.Picture != null)
            ////{
            ////    xrpPicture.Image = byteArrayToImage(info.Properties.Picture);
            ////}
            DateTime PoliticsJoinDate;
            if (DateTime.TryParse(info.Properties.PoliticsJoinDate.ToString(), out PoliticsJoinDate))
            {
                xrlPoliticsJoinDate.Text = PoliticsJoinDate.ToShortDateString();
            }
            else
            {
                xrlPoliticsJoinDate.Text = "";
            }
            xrlCellPhone.Text = info.Properties.CellPhone;
            xrlHomePhone.Text = info.Properties.HomePhone;
            xrlAddr.Text = info.Properties.Addr;
            xrlTempAddr.Text = info.Properties.TempAddr;
            xrlEmail.Text = info.Properties.Email;
            if (info.Properties.BeginDateWork != null)
            {
                DateTime BeginDateWork;
                if (DateTime.TryParse(info.Properties.BeginDateWork.Value.ToString(), out BeginDateWork))
                {
                    xrlBeginDateWork.Text = BeginDateWork.ToShortDateString();
                }
                else
                {
                    xrlBeginDateWork.Text = "";
                }
            }
            xrlNotes.Text = info.Properties.Notes;

            Catalogue_NationInfo infoNation = new Catalogue_NationInfo(Convert.ToInt32(info.Properties.Catalogue_NationKey));
            Catalogue_CityInfo infoCity = new Catalogue_CityInfo(Convert.ToInt32(info.Properties.Catalogue_CityKey));
            Catalogue_DistrictInfo infoDistrict = new Catalogue_DistrictInfo(Convert.ToInt32(info.Properties.Catalogue_DistrictKey));
            Catalogue_EthnicInfo infoEthnic = new Catalogue_EthnicInfo(Convert.ToInt32(info.Properties.Catalogue_EthnicKey));
            Catalogue_ReligionInfo infoReligion = new Catalogue_ReligionInfo(Convert.ToInt32(info.Properties.Catalogue_ReligionKey));
            Catalogue_PoliticInfo infoPolitic = new Catalogue_PoliticInfo(Convert.ToInt32(info.Properties.Catalogue_PoliticsKey));
            Catalogue_PositionInfo infoPosition = new Catalogue_PositionInfo(Convert.ToInt32(info.Properties.Catalogue_PositionKey));
            Catalogue_DegreeInfo infoDegree = new Catalogue_DegreeInfo(Convert.ToInt32(info.Properties.Catalogue_DegreeKey));
            Catalogue_DepartmentInfo infoDeparment = new Catalogue_DepartmentInfo(Convert.ToInt32(info.Properties.Catalogue_DepartmentKey));
            if (infoNation.Properties.Name != null)
            {
                xrlNation.Text = infoNation.Properties.Name;
            }
            else
            {
                xrlNation.Text = "";
            }
            if (infoCity.Properties.Name != null)
            {
                xrlCity.Text = infoCity.Properties.Name;
            }
            else
            {
                xrlCity.Text = "";
            }
            if (infoDistrict.Properties.Name != null)
            {
                xrlDistrict.Text = infoDistrict.Properties.Name;
            }
            else
            {
                xrlDistrict.Text = "";
            }
            if (infoEthnic.Properties.Name != null)
            {
                xrlEthnic.Text = infoEthnic.Properties.Name;
            }
            else
            {
                xrlEthnic.Text = "";
            }
            if (infoReligion.Properties.Name != null)
            {
                xrlReligion.Text = infoReligion.Properties.Name;
            }
            else
            {
                xrlReligion.Text = "";
            }
            if (infoPolitic.Properties.Name != null)
            {
                xrlPolitics.Text = infoPolitic.Properties.Name;
            }
            else
            {
                xrlPolitics.Text = "";
            }
            if (infoDegree.Properties.Name != null)
            {
                xrlDegree.Text = infoDegree.Properties.Name;
            }
            else
            {
                xrlDegree.Text = "";
            }
            if (infoDeparment.Properties.Name != null)
            {
                xrlDepartment.Text = infoDeparment.Properties.Name;
            }
            else
            {
                xrlDepartment.Text = "";
            }
            if (infoPosition.Properties.Name != null)
            {
                xrlPosition.Text = infoPosition.Properties.Name;
            }
            else
            {
                xrlPosition.Text = "";
            }

        }
        public RptPersonel(string ID, string Name, string IdentityNo="", string Birthday ="", string Gender="", string PlaceOfBirth="",
            string Polictic="", string CellPhone="", string HomePhone="", string Addr="", string TempAddr="", string Email="",
            string BeginDateWork="", string Notes="", string Nation="", string City="", string District="",string Ethnic="",
            string Religion="", string Pilictic="", string Degree="", string Department="", string Position="", string PoliticsJoinDate="" , byte[] Picture=null)
        {
            xrlID.Text = ID;
            xrlLastName.Text = Name;
            xrlIdentityNo.Text = IdentityNo;
            xrlBirthday.Text = Birthday;
            xrlGender.Text = Gender;
            xrlPlaceOfBirth.Text = PlaceOfBirth;
            xrpPicture.Image = byteArrayToImage(Picture);
            xrlPoliticsJoinDate.Text = PoliticsJoinDate;
            xrlCellPhone.Text = CellPhone;
            xrlHomePhone.Text = HomePhone;
            xrlAddr.Text = Addr;
            xrlTempAddr.Text = TempAddr;
            xrlEmail.Text = Email;
            xrlBeginDateWork.Text = BeginDateWork;
            xrlNotes.Text = Notes;
            xrlNation.Text = Nation;
            xrlCity.Text = City;
            xrlDistrict.Text = District;
            xrlEthnic.Text = Ethnic;
            xrlReligion.Text = Religion;
            xrlPolitics.Text = Polictic;
            xrlDegree.Text = Degree;
            xrlDepartment.Text = Department;
            xrlPosition.Text = Position;
        }
        private Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }

    }
}
